/***************************************************************
 *
 * (c) 2008 Markus Dittrich 
 *
 * This program is free software; you can redistribute it 
 * and/or modify it under the terms of the GNU General Public 
 * License Version 3 as published by the Free Software Foundation. 
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License Version 3 for more details.
 *
 * You should have received a copy of the GNU General Public 
 * License along with this program; if not, write to the Free 
 * Software Foundation, Inc., 59 Temple Place - Suite 330, 
 * Boston, MA 02111-1307, USA.
 *
 ****************************************************************/

/* STL includes */

/* local includes */
#include "canvas_to_datamap.h"
#include "utilities.h"

//----------------------------------------------------------------
// constructor
//----------------------------------------------------------------
CanvasToDataMap::CanvasToDataMap(int width, int height)
  :
    PointStore_(CanvasMap(boost::extents[width][height])),
    Width_(width),
    Height_(height)
{
  for ( int xPos = 0; xPos < Width_; ++xPos)
  {
    for ( int yPos = 0; yPos < Height_; ++yPos)
    {
      PointStore_[xPos][yPos] = DataPoint(QString(""),0);
    }
  }
}


//----------------------------------------------------------------
// insert an array of QPoints corresponding to data set dataName 
// into the canvas map.
// NOTE: We discard all data points that are currently not
// on the canvas, e.g. during zooming
//----------------------------------------------------------------
void CanvasToDataMap::insert(const QPoint* dataPoints, int numItems,
    const QString& dataName)
{
  for ( int counter = 0; counter < numItems; ++counter)
  {
    int x = dataPoints[counter].x();
    int y = dataPoints[counter].y();

    if ( (x < 0) || (x >= Width_)
      || (y < 0) || (y >= Height_) )
    {
      continue;
    }

    PointStore_[x][y] = DataPoint(dataName, counter);
  }
}


//----------------------------------------------------------------
// retrieve a particular data point at position (xPos, yPos) if
// present
//----------------------------------------------------------------
const DataPoint& CanvasToDataMap::retrieve(int xPos, int yPos) const
{  
  assert ( (xPos >= 0) && (xPos <= Width_) );
  assert ( (yPos >= 0) && (yPos <= Height_) );

  return PointStore_[xPos][yPos];
}


//----------------------------------------------------------------
// clear the canvas map
//----------------------------------------------------------------
void CanvasToDataMap::clear() 
{
  for ( int xPos = 0; xPos < Width_; ++xPos)
  {
    for ( int yPos = 0; yPos < Height_; ++yPos)
    {
      PointStore_[xPos][yPos] = DataPoint(QString(""),0);
    }
  }
}
  
